<?php

namespace app\adminapi\controller\v1\marketing;

use app\adminapi\controller\AuthController;
use app\adminapi\export\StoreFreeExport;
use app\services\activity\free\StoreFreeServices;
use app\services\product\sku\StoreProductAttrValueServices;
use crmeb\exceptions\AdminException;
use crmeb\services\CacheService;
use think\facade\App;

/**
 * 限时满赠  控制器
 * Class StoreSeckill
 * @package app\admin\controller\store
 */
class StoreFree extends AuthController
{
    public function __construct(App $app, StoreFreeServices $services)
    {
        parent::__construct($app);
        $this->services = $services;
    }

    /**
     * 显示资源列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $where = $this->request->getMore([
            [['time_status', 's'], ''],
            [['title', 's'], ''],
            ['limit', 15]
        ]);
        return app('json')->success($this->services->systemPage($where));
    }

    public function export()
    {
        $where = $this->request->getMore([
            [['time_status', 's'], ''],
            [['title', 's'], ''],
            ['limit', 15]
        ]);
        return app()->make(StoreFreeExport::class)->export($where);
    }


    public function detail($id)
    {
        return app('json')->success($this->services->getDetail($id));
    }

    /**
     * 设置批量商品上架
     * @return mixed
     */
    public function free_show()
    {
        [$ids] = $this->request->postMore([
            ['ids', []]
        ], true);
        $this->services->setShow($ids, 1);
        return app('json')->success(100014);
    }

    /**
     * 设置批量商品下架
     * @return mixed
     */
    public function free_unshow()
    {
        [$ids] = $this->request->postMore([
            ['ids', []]
        ], true);
        $this->services->setShow($ids, 0);
        return app('json')->success(100014);
    }

    /**
     * 详情
     * @param $id
     * @return mixed
     */
    public function read($id)
    {
        $info = $this->services->getInfo($id);
        return app('json')->success(compact('info'));
    }

    /**
     * 保存满赠商品
     * @param int $id
     */
    public function save($id)
    {
//        $data = $this->request->postMore([
//            [['product_id', 'd'], 0],
//            [['title', 's'], ''],
//            [['info', 's'], ''],
//            [['unit_name', 's'], ''],
//            ['images', []],
//            [['give_integral', 'd'], 0],
//            ['section_time', []],
//            [['is_hot', 'd'], 0],
//            [['status', 'd'], 0],
//            [['num', 'd'], 0],
//            [['once_num', 'd'], 0],
//            [['time_id', 'd'], 0],
//            [['temp_id', 'd'], 0],
//            [['sort', 'd'], 0],
//            [['description', 's'], ''],
//            ['attrs', []],
//            ['items', []],
//            ['copy', 0],
//            ['logistics', []],//物流方式
//            ['freight', 1],//运费设置
//            ['postage', 0],//邮费
//            ['custom_form', ''],
//            ['virtual_type', 0],
//            ['free_type', 0],//满赠类型
//            ['free_value', 0],//满赠值（金额|件）
//        ]);

        $data = $this->request->postMore([
            ['title', ''], // 活动名称
            ['start_time', 0], // 开始时间
            ['end_time', 0], // 结束是按
            ['add_marketing_type', -1], // 0. 不叠加活动, 1. 叠加秒杀， 2. 优惠券， 3. 秒杀+优惠券
            ['is_use_all_product', -1], // 是否适用所有商品(1. 是，0.否)
            ['productRelations', []], // 适用商品
            ['rules', []],  // 所属规则
            ['type', 0], // 1. 满额。2.满赠
        ]);

        try {
            $this->validate($data, \app\adminapi\validate\marketing\StoreFreeValidate::class, 'save');
        } catch (\Exception $exception) {
            throw new AdminException($exception->getMessage());
        }

        $this->services->saveData($id, $data);
        return app('json')->success(100000);
    }

    /**
     * 删除满赠
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        if (!$id) return app('json')->fail(100100);
        $this->services->update($id, ['is_del' => 1]);
        /** @var StoreProductAttrValueServices $storeProductAttrValueServices */
        $storeProductAttrValueServices = app()->make(StoreProductAttrValueServices::class);
        $unique = $storeProductAttrValueServices->value(['product_id' => $id, 'type' => 1], 'unique');
        if ($unique) {
            CacheService::delete('seckill_' . $unique . '_1');
        }
        return app('json')->success(100002);
    }

    /**
     * 修改状态
     * @param $id
     * @param $status
     * @return mixed
     */
    public function set_status($id, $status)
    {
        $this->services->update($id, ['status' => $status]);
        return app('json')->success(100014);
    }

    /**
     * 满赠时间段列表
     * @return mixed
     */
    public function time_list()
    {
        $list['data'] = sys_data('routine_seckill_time');
        return app('json')->success(compact('list'));
    }

    /**
     * 满赠统计
     * @param $id
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function freeStatistics($id)
    {
        $data = $this->services->seckillStatistics($id);
        return app('json')->success($data);
    }

    /**
     * 满赠参与人统计
     * @param $id
     * @return mixed
     */
    public function freePeople($id)
    {
        [$keyword] = $this->request->getMore([
            ['real_name', '', '', 'keyword']
        ], true);
        return app('json')->success($this->services->seckillPeople($id, $keyword));
    }

    /**
     * 满赠订单统计
     * @param $id
     * @return mixed
     */
    public function freeOrder($id)
    {
        $where = $this->request->getMore([
            ['real_name', ''],
            ['status', '']
        ]);
        return app('json')->success($this->services->seckillOrder($id, $where));
    }
}
